package com.learn.UnSafeCollection;

import java.util.*;
import java.util.concurrent.CopyOnWriteArraySet;

//  解决并发修改异常 java.util.ConcurrentModificationException !!

public class SetTest {

    public static void main(String[] args) {

        /**
         * 同理:Set set = new HashSet();  不安全
         * 1.Set set = Collections.synchronizedSet(new HashSet<>());
         * 2.Set set = new CopyOnWriteArraySet();
         *
         * 由此可以引申出 什么是HashSet?
         * 其实就是HashSet底层就是new HasmMap()
         * 那么什么是HashMap  牢记( 负载因子 和 初始值 )
         *
         *
         *
         */
        Set set = new CopyOnWriteArraySet();

        for (int i = 1; i <= 10; i++) {
            new Thread(()->{
                String substring = UUID.randomUUID().toString().substring(0,5);
                set.add(substring);
                System.out.println(set);
            },String.valueOf(i)).start();
        }
    }
}
